package com.sunshine.lnuplus.ui.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import com.sunshine.lnuplus.R
import com.sunshine.lnuplus.base.BaseActivity
import com.sunshine.lnuplus.model.InitData
import com.sunshine.lnuplus.utils.BandRUtils
import com.sunshine.lnuplus.utils.StatusBarUtils
import com.sunshine.lnuplus.utils.TagHelper
import es.dmoral.toasty.Toasty
import kotlinx.android.synthetic.main.activity_b_and_r.*
import kotlinx.android.synthetic.main.activity_calender.arcView_calender


class BandRActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_b_and_r)

        if(InitData.lessonList == null){
            startActivity(Intent(this, SplashActivity::class.java))
            finish()
        }
        initView()
    }

    private fun initView() {
        StatusBarUtils.setStatusBarWithColor(this, color)
        arcView_calender.setColor(color, color)
        title = "备份/恢复"

        button_b_json.setOnClickListener {
            val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
                addCategory(Intent.CATEGORY_OPENABLE)
                type = "application/octet-stream"
                putExtra(Intent.EXTRA_TITLE, "lnuplus_back.json")
            }
            startActivityForResult(intent, TagHelper.BANDRB)
        }
        button_r_json.setOnClickListener {
            val intent = Intent(Intent.ACTION_GET_CONTENT)
            intent.type = "*/*"
            intent.addCategory(Intent.CATEGORY_OPENABLE)
            startActivityForResult(intent, TagHelper.BANDRR)
        }
        button_b_ics.setOnClickListener {
            val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
                addCategory(Intent.CATEGORY_OPENABLE)
                type = "application/octet-stream"
                putExtra(Intent.EXTRA_TITLE, "lnuplus_back.ics")
            }
            startActivityForResult(intent, TagHelper.BANDRBICS)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when(requestCode){
            TagHelper.BANDRB -> {
                if(resultCode == Activity.RESULT_OK){
                    if(BandRUtils.b(data?.data, this)){
                        Toasty.success(this, "导出成功~").show()
                    }else{
                        Toasty.error(this, "导出失败，你可以参考“常见问题”或联系开发者").show()
                    }
                }
            }
            TagHelper.BANDRR -> {
                if(resultCode == Activity.RESULT_OK){
                    when(BandRUtils.r(data?.data, this)){
                        0 -> Toasty.error(this, "文件打开失败").show()
                        1 -> Toasty.error(this, "非json类型文件").show()
                        2 -> {
                            Toasty.success(this, "导入成功").show()
                            setResult(TagHelper.BANDRR)
                            finish()
                        }
                        3 -> Toasty.error(this, "解析失败").show()
                    }
                }
            }
            TagHelper.BANDRBICS -> {
                if(resultCode == Activity.RESULT_OK){
                    if(BandRUtils.bIcs(data?.data, this)){
                        Toasty.success(this, "导出成功~").show()
                    }else{
                        Toasty.error(this, "导出失败，你可以参考“常见问题”或联系开发者").show()
                    }
                }
            }
        }
    }
}
